<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html 
	xmlns="http://www.w3.org/1999/xhtml" 
	xmlns:xf="http://www.w3.org/2002/xforms"
	xmlns:ev="http://www.w3.org/2001/xml-events"
	xmlns:eb-forms="http://code.google.com/p/eb-forms">
	
	<head>
		<title>Client</title>
		<!--
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta name="Author" content="Hampei" />
		<meta name="Author" content="B. Broeksema" />
		-->
		
		<link href="style.css" rel="stylesheet" type="text/css"/>
		
		<script type="text/javascript" src="js/prototype.js"></script>
		
		<script type="text/javascript">
			//<![CDATA[
			var toolApplet = null;
			var xmlDoc = null;
			/** 
			* Initialize annotationFileEditor. 
			* Will be called by the toolApplet when that has finished loading. 
			* @param applet Reference to the toolApplet. 
			*/
			//var guiframe;
			function appletLoaded( tapplet ) {
				//alert ('applet loaded');
				toolApplet = tapplet;
			}
			
			function getMessageList() {
				frames['guiframe'].location.href = "messages.xhtml";
			}
			
			function showIncommingMessage() {
				toolApplet.transformEBSchema( 'tf-read.xml' );
				// This should become a bit more dynamic maybe. The output location is
				// specified in tf-read.xml (see also messages.xhtml).
				frames['guiframe'].location.href = 
					'../../prototype-gs1/output/message.xhtml';
				
				loadData( "../tf-read.xml" );
			}
			
			function loadData( fileName ) {
				xmlDoc = document.implementation.createDocument("", "", null);
				xmlDoc.onload = getFileLocation;
				xmlDoc.load( fileName );
			}
			
			function getFileLocation() {
				var labels = xmlDoc.getElementsByTagName('file');
				var sLocation = labels[0].getAttribute('location');
				fetchFile( sLocation );
				//alert( fileName );
			}
			
			function fetchFile( sLocation ) {
				netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
				new Ajax.Request( sLocation, {
					method: 
						'get',
						onSuccess: function(transport) {
							var fileName = sLocation.split( '=' )[1];
							toolApplet.saveFile('inbox/'+fileName, transport.responseText);
						},
						onFailure: function() {
							alert( "get file failed" );
						}
					}
				);
			}
			
			function newOrder( ) {
				var order_config;
				if (!!(window.attachEvent && !window.opera)) { //ie
					order_config = 'transform-config-ie.xml'
						resultPath = "../../prototype-gs1/output"
							+ "/FoodservicePurchaseOrder.html";
				} else {
					order_config = 'transform-config.xml'
					resultPath = "../../prototype-gs1/output"
						+ "/FoodservicePurchaseOrder.xhtml";
				}		
			
				toolApplet.transformEBSchema( order_config );
				
				frames['guiframe'].location.href = resultPath;
			}
			/**
			* Serialize an XML Document or Element and return it as a string.
			*/
			XML.serialize = function(node) {
				if (typeof XMLSerializer != "undefined")
					return (new XMLSerializer()).serializeToString(node);
				else if (node.xml) 
					return node.xml;
				else 
					throw "XML.serialize is not supported or can't serialize " + node;
			}
				
			function sendMessage() {
				var model = frames['guiframe'].document.getElementById("M-1");
				var instance = model.getInstanceDocument("instance.message");
				
				/*
				alert( 'var instance is een XMLDocument object. Deze moet op de\n' +
					'een of andere manier naar de server gezonden worden!' + 
					XML.serialize(instance));
				*/
				
				netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
				//new Ajax.Request( "http://localhost:8088/place_order", {
				new Ajax.Request( "http://10.122.91.22:8088/place_order", {
					method: 'post',
					postBody: XML.serialize(instance),
					contentType: 'text/xml',
					onSuccess: function(transport) {
						alert( "Bericht verzonden" );
						frames['guiframe'].location.href = "../../ebforms-xslt/gui/messages.xhtml";
					},
					onException: function(req, exeption) {
						alert(exeption);
					},
					onFailure: function(req, resp) {
						alert( "Fout bij verzenden van bericht" + resp);
					}
				});
			}
			
			//]]>
		</script>
	</head>
	<body>
		<div>
			<applet
				id="toolapplet" codebase=".." code="com.flowcanto.client.ToolApplet"
				archive="java/lib/toolapplet.jar, ../lib/saxon8.jar, java/lib/plugin.jar"
				mayscript="true" width="0" height="0">
				<param name="cache_option" value="No" />
			</applet>
		</div>

		<div id="menu">
			<a href="#" onclick="newOrder()">Nieuwe Order</a>
			<a href="#" onclick="getMessageList()">Inbox</a>
		</div>

		<iframe id="guiframe" src="messages.xhtml">
			<h1>Welkom</h1>
		</iframe>
	</body>
</html>